﻿using ModernChannel.Model.Interface;
using ModernChannel.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Util;

namespace ModernChannel.Provider
{
    public class SerialNumberCdcHistProvider : GenericProvider, ISerialNumberCdcHistProvider
    {
        public List<SerialNumberCdcHist> SearchSerialNumberCdcHist(string serial, string model, string sellOutFlag = null, int? serialNo = null)
        {
            var query = repository.GetQuery<SerialNumberCdcHist>();
            if (!string.IsNullOrWhiteSpace(sellOutFlag))
            {
                query = query.Where(x => x.SelloutFlag.Equals(sellOutFlag));
            }

            if (serialNo.HasValue)
            {
                query = query.Where(x => Int32.Parse(x.SerialNo.Substring(1, 3)) >= serialNo.Value);
            }

            if (!serial.IsNullOrWhiteSpace())
            {
                query = query.Where(x => x.SerialNo.ToUpper() == serial.ToUpper());
            }

            if (!model.IsNullOrWhiteSpace())
            {
                query = query.Where(x => x.Model.ToUpper() == model.ToUpper());
            }

            return query.ToList();
        }


        public bool Update(SerialNumberCdcHist obj)
        {
            try
            {
                var updateRecord = Retrieve(obj.Model, obj.Suffix, obj.SerialNo);
                if (updateRecord == null) return false;

                updateRecord.BoxNo = obj.BoxNo;
                updateRecord.ClosedDate = obj.ClosedDate;
                updateRecord.CustomerCode = obj.CustomerCode;
                updateRecord.CustomerName = obj.CustomerName;
                updateRecord.InvoiceDate = obj.InvoiceDate;
                updateRecord.InvoiceNo = obj.InvoiceNo;
                updateRecord.LoadplanNo = obj.LoadplanNo;
                updateRecord.ModelSuffix = obj.ModelSuffix;
                updateRecord.SelloutDate = obj.SelloutDate;
                updateRecord.SerialNo = obj.SerialNo;
                updateRecord.ShiptoDate = obj.ShiptoDate;
                updateRecord.ShiptoName = obj.ShiptoName;
                updateRecord.UniqueId = obj.UniqueId;
                updateRecord.WarrantyIfDate = obj.WarrantyIfDate;
                updateRecord.IncentiveFlag = obj.IncentiveFlag;
                updateRecord.SelloutFlag = obj.SelloutFlag;
                updateRecord.WarrantyIfFlag = obj.WarrantyIfFlag;
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public SerialNumberCdcHist Retrieve(string models, string suffixes, string serialNumber)
        {

            var model = (models ?? string.Empty).ToUpper();
            var suffix = (suffixes ?? string.Empty).ToUpper();
            var serialNo = (serialNumber ?? string.Empty).ToUpper();

            var updateRecord = repository.FindOne<SerialNumberCdcHist>(k => k.SerialNo.ToUpper() == serialNo && k.Model.ToUpper() == model && k.Suffix.ToUpper() == suffix);
            return updateRecord;
        }
    }
}
